<template>
  <div class="login">
    <div class="login_form">
      <span class="light"></span><span class="light"></span><span class="light"></span><span class="light"></span>
      <div class="welcome">欢迎登录</div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="$rules.login"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
            <i slot="prefix" class="el-input__icon el-icon-user" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
          >
            <i slot="prefix" class="el-input__icon el-icon-lock" />
          </el-input>
        </el-form-item>
        <el-checkbox
          v-model="loginForm.rememberMe"
          style="margin: 0px 0px 25px 0px; color: #fff"
          >记住密码</el-checkbox
        >
        <el-form-item style="width: 100%">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="
              width: 100%;
              background: #46b1e3;
              border-radius: 15px;
              font-size: 14px;
            "
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="dateTime">
      <div class="date">北京时间：{{ date }}</div>
      <div class="time">{{ time }}</div>
    </div>
  </div>
</template>
<script>
import { getCurrentDate, getCurrentTime } from "@/utils/date.js";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import aes from "@/utils/aes";
export default {
  name: "login",
  components: {},
  data() {
    return {
      date: "",
      time: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
      },
      loading: false,
      redirect: undefined,
    };
  },
  created() {
    this.date = getCurrentDate();
    this.time = getCurrentTime();
    this.getCookie();
    setInterval(() => {
      this.time = getCurrentTime();
    }, 1000);
  },
  mounted() {},
  methods: {
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 4320 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 4320,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 4320,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.loginForm.password = aes.encrypt(this.loginForm.password);
          this.$store
            .dispatch("login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
              // console.log(222);
            })
            .catch(() => {
              this.loading = false;
              this.loginForm.password = aes.decrypt(this.loginForm.password);
            });
        }
      });
    },
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
};
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url("../assets/login/login_bg2.png") center no-repeat;
  background-size: 100% 100%;
  position: relative;
  .login_form {
    position: absolute;
    right: r(150);
    top: r(300);
    background: url("../assets/login/login_box.png") center no-repeat;
    background-size: 100% 100%;
    overflow: hidden;
    .welcome {
      width: r(400);
      height: r(60);
      text-align: center;
      line-height: r(60);
      font-size: r(26);
      color: #fff;
    }
    .login-form {
      width: r(320);
      padding: r(10) r(40);
      .el-input {
        height: 40px;
        input {
          height: 40px;
        }
      }
      .input-icon {
        height: 38px;
        width: 14px;
        margin-left: 2px;
      }
    }
    .light {
      position: absolute;
      display: block;
    }
    .light:nth-child(1) {
      filter: hue-rotate(0deg);
      top: 0;
      left: 0;
      width: 100%;
      height: 3px;
      background: linear-gradient(90deg, transparent, #3a86ff);
      animation: animate1 8s linear infinite;
    }

    @keyframes animate1 {
      0% {
        left: -100%;
      }

      50%,
      100% {
        left: 100%;
      }
    }
    .light:nth-child(2) {
      filter: hue-rotate(60deg);
      top: -100%;
      right: 0;
      width: 3px;
      height: 100%;
      background: linear-gradient(180deg, transparent, #3a86ff);
      animation: animate2 8s linear infinite;
      animation-delay: 2s;
    }

    @keyframes animate2 {
      0% {
        top: -100%;
      }

      50%,
      100% {
        top: 100%;
      }
    }
    .light:nth-child(3) {
      filter: hue-rotate(120deg);
      bottom: 0;
      right: 0;
      width: 100%;
      background: linear-gradient(270deg, transparent, #3a86ff);
      animation: animate3 8s linear infinite;
      animation-delay: 4s;
    }

    @keyframes animate3 {
      0% {
        right: -100%;
        height: 3px;
      }

      50%,
      100% {
        height: 2px;
        right: 100%;
      }
    }
    .light:nth-child(4) {
      filter: hue-rotate(300deg);
      bottom: -100%;
      left: 0;
      width: 3px;
      height: 100%;
      background: linear-gradient(360deg, transparent, #3a86ff);
      animation: animate4 8s linear infinite;
      animation-delay: 6s;
    }

    @keyframes animate4 {
      0% {
        bottom: -100%;
      }

      50%,
      100% {
        bottom: 100%;
      }
    }
  }
  .dateTime {
    position: absolute;
    bottom: r(100);
    left: r(150);
    width: r(300);
    height: r(100);
    .date {
      font-size: r(22);
      color: #0bdef3;
      line-height: r(30);
    }
    .time {
      font-size: r(86);
      color: #0bdef3;
      line-height: r(70);
      font-family: "LetsgoDigitalRegular";
    }
  }
}
</style>
